#include "lock/lock.h"
#include "dev/vio.h"
#include "fs/base_buf.h"
#include "lib/str.h"
#include "lib/print.h"
#include "memlayout.h"


static spinlock_t ramdisk_lock;
static uint32 sector_max;

void ram_disk_init(void)
{
    sector_max = (2ul * 1024ul * 1024ul * 1024ul) / SECTOR_SIZE;
    spinlock_init(&ramdisk_lock, "ramdisk");
}

void ram_disk_rw(buf_t* buf, bool write)
{
    assert(buf && buf->sector < sector_max, "ram_disk_rw");
    spinlock_acquire(&ramdisk_lock);
    void* sector_addr = (void*)SDCARD_BEGIN + buf->sector * SECTOR_SIZE;
    if(write) {
        memmove(sector_addr, buf->data, SECTOR_SIZE);
    } else {
        memmove(buf->data, sector_addr, SECTOR_SIZE);
    }
    spinlock_release(&ramdisk_lock);
}

void ram_disk_intr()
{
    panic("ram_disk_intr");
}
